var _param={
    init:function(){
        this.goodListJson = $("#data-goodList").text().trim();
        this.goodList = $.evalJSON($("#data-goodList").text().trim());
        this.deedId = $.evalJSON($("#data-deedId").text().trim());
        this.merchantId = $.evalJSON($("#data-merchantId").text().trim());
        this.parentMerchant = $.evalJSON($("#data-parentMerchant").text().trim());
        this.merchant = $.evalJSON($("#data-merchant").text().trim());
        this.purchaseList = $.evalJSON($("#data-purchaseList").text().trim());
        _goodEntry.storageId=this.deedId+"_"+this.merchantId;
    },
    goodList:null,
    goodListJson:null,
    deedId:null,
    merchantId:null,
    merchant:null,
    parentMerchant:null,
    purchaseList:null
};
var _leftNav={
    init:function(){
        this.operation.init();
    },
    operation:{
        init:function(){
          this.merchantItem();
        },
        merchantItem:function(){
           $("body").on('click','.merchant_item',function(){
              var id=$(this).attr("data-id");
              window.location.href=baseURL+"web/goodEntry/"+_param.deedId+"/"+id;
           });
        }
    }
};
var _goodEntry={
   init:function(){
         this.operation.init();
         this.blind();
   },
   bean:{
       list:[]
   },
   purchase:{
     goodName:null,
     goodId:null,
     number:null,
     remark:null
   },
   operation:{
      init:function(){
          this.suggest();
          this.add();
          this.remove();
          this.edit();
          this.save();
      },
      suggest:function(){
          $("#goodInput").bsSuggest({
              //url: "/rest/sys/getuserlist?keyword=",
              //url: "data.json",
              data: {value:_param.goodList},
              effectiveFields: ["name", "pinyin"],
              searchFields: [ "name","pinyin"],
              effectiveFieldsAlias:{name: "菜品名",pinyin:"拼音"},
              ignorecase: true,
              number:true,
              // showHeader: true,
               // showBtn: false,     //不显示下拉按钮
              //delayUntilKeyup: true, //获取数据的方式为 firstByUrl 时，延迟到有输入/获取到焦点时才请求数据
              idField: "id",
              keyField: "name"
          }).on('onDataRequestSuccess', function (e, result) {
              //console.log('onDataRequestSuccess: ', result);
          }).on('onSetSelectValue', function (e, keyword, data) {
              $("#goodInput").attr("data-name",data.name);
              //console.log('onSetSelectValue: ', keyword, data);
          }).on('onUnsetSelectValue', function (e) {
             // console.log("onUnsetSelectValue");
          });
      },
      add:function(){
          $("#number").keydown(function (event) {
              if (event.keyCode == 13) {
                  var number=$("#number").val();
                  if(null!=_Base.init(number)&&parseFloat(number)>0){
                      $("#add").click();
                  }
              }
          });
          $("#add").click(function(){
              var id=  $("#goodInput").attr("data-id");
              if(null==_Base.init(id)){
                  layer.error("请输入菜品");
                  return;
              }
              var number=$("#number").val();
              if(null==_Base.init(number)){
                  layer.error("请输入数量");
                  return;
              }
              if(parseFloat(number)<=0){
                  layer.error("请输入大于0的数字");
                  return;
              }
              var name=$("#goodInput").attr("data-name");
              var item=_Base.clone(_goodEntry.purchase);
              item.goodId=id;
              item.goodName=name;
              item.number=number;
              item.merchantId=_param.merchantId;
              item.merchantName=_param.merchant.name;
              item.deedId=_param.deedId;
              if(null!=_param.parentMerchant){
                  item.parentMerchantId=_param.parentMerchant.id;
                  item.parentMerchantName=_param.parentMerchant.name;
              }else{
                  item.parentMerchantId=_param.merchantId;
                  item.parentMerchantName=_param.merchant.name;
              }
              var param=item;
              $.post(baseURL+"purchase/add.do",param,function(data){
                  _Base.result(data,function(){
                      item.id=data.result.id;
                      _goodEntry.bean.list.push(ko.mapping.fromJS(item));
                      $("#goodInput").val("");
                      $("#number").val("");
                      $("#goodInput").attr("data-id","");
                      $("#goodInput").attr("data-name","");
                      $("#goodInput").focus();
                  });
              });
              //存储
             // _goodEntry.storage();
          });
      },
      getIndex:function(item){
          var index= $(item).parents(".item").attr("data-index");
          return index;
      },
      remove:function(){
          $("body").on('click','.delete',function(){
              var index=_goodEntry.operation.getIndex($(this));
              layer.confirm("确定删除?",function(){
                  $.post(baseURL+"purchase/delete.do",{id:_goodEntry.bean.list()[index].id()},function(data){
                      _goodEntry.bean.list.remove(_goodEntry.bean.list()[index]);
                  });
                  //_goodEntry.storage();
              });
          });

      },
      edit:function(){
           $("body").on('click','.change',function(){
               var index=_goodEntry.operation.getIndex($(this));
               _goodEntry.bean.list()[index].isEdit(true);
           });

      },
      save:function(){
          $("body").on('click','.save',function(){
              var index=_goodEntry.operation.getIndex($(this));
              var item=_goodEntry.bean.list()[index];
              if(null==_Base.init(item.number())||parseFloat(item.number())<=0){
                  layer.error("请输入正确的数量！");
                  return;
              }
              var param=ko.mapping.toJS(item);
              $.post(baseURL+"purchase/update",param,function(data){
                  item.isEdit(false);
              });
              //_goodEntry.storage();
          });
      }
   },
   storageId:null,
   storage:function(){
       //还得加ID
       var bean=ko.mapping.toJS(_goodEntry.bean);
       var json= $.toJSON(bean.list);
       localStorage.setItem(this.storageId,json);
   },
   workItem:function(item){
        item.isEdit = ko.observable(false);
   },
   blind:function(){
       var list=[];
       //var json=localStorage.getItem(this.storageId);
       if(null!=_Base.init(_param.purchaseList)){
           list= _param.purchaseList;
       }
       _goodEntry.bean.list=ko.mapping.fromJS(list);
       ko.applyBindings(_goodEntry.bean, $("#purchase_box")[0]);
   }
};
var _export={
    init:function(){
          this.exportDo();
    },
    exportDo:function(){
       $("body").on('click','.export',function(){
          var deedId=$(this).attr("data-id");
           var param={
               deedId:deedId
           };
          layer.waiting("正在生成中");
          $.post(baseURL+"purchase/export",param,function(data){
               console.dir(data);
               _Base.result(data,function(){
                  layer.closeAll();
                 window.location.href=baseURL+data.result;
               });
           });
       });
    }
};
var _aa=null;
$(function(){

    _param.init();
    _leftNav.init();
    _goodEntry.init();
    _export.init();
});